home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
MATH
/
MATH1
/
SORT-Q-R.LIB
< prev
next >
Wrap
Text File
|
1985-04-03
|
1KB
|
58 lines
{ --> 180}
procedure {quick} sort(var x: ary; n: integer);
{ a RECURSIVE sorting routine }
{ Adapted from 'The design of Well-Structured and Correct Programs',
S. Alagic, Springer-Verlag, 1978 }
procedure qsort(var x: ary; m,n: integer);
var i,j : integer;
procedure partit(var a: ary; var i,j: integer; left,right: integer);
var pivot : real;
procedure swap(var p,q: real);
var hold : real;
begin
hold:=p;
p:=q;
q:=hold
end; { swap }
begin
pivot:=a[(left+right)div 2];
i:=left;
j:=right;
while i<=j do
begin
while a[i]<pivot do
i:=i+1;
while pivot<a[j] do
j:=j-1;
if i<=j then
begin
swap(a[i],a[j]);
i:=i+1;
j:=j-1
end
end { while }
end { partit }
begin { q-sort }
if m<n then
begin
partit(x,i,j,m,n); { divide in two }
qsort(x,m,j); { sort left part }
qsort(x,i,n) { sort right part }
end
end; { QSORT }
begin { sort }
qsort(x,1,n)
end; { SORT }